/**
 * @Description: 系统安全设置
 * @author randy
 * @date 2019/11/21 下午17:21
 */
import {
  Button,
  Form,
  Input,
  Select,
  Upload,
  message,
  Col,
  Row,
  AutoComplete,
  Radio,
  Checkbox,
  Divider,
  Card,
  Typography,
  notification,
  InputNumber,
  Icon,
  Tooltip,
  Spin,
} from 'antd';
import Modal from 'drag-modal';
// eslint-disable-next-line import/no-extraneous-dependencies
import { FormattedMessage, formatMessage } from 'umi-plugin-react/locale';
import React, { Component, Fragment } from 'react';
import { connect } from 'dva';
import { rulesReg } from '../../../utils/uiUtils';
import { getWebPath } from '../../../utils/request';
import { requestLists, requestPromise } from '@/utils/request';

const FormItem = Form.Item;
const { Option } = Select; // 头像组件 方便以后独立，增加裁剪之类的功能
const { Text } = Typography;

@connect(({ accountAndsettings }) => ({
  // currentUser: accountAndsettings.currentUser,
  currentUser: '',
}))
class SafeSetting extends Component {
  view = undefined;

  constructor(props) {
    super(props);
    this.state = {
      safe: {},
    };
  }

  componentDidMount() {
    this.setState({ loading: true });
    this.getMailInfo();
  }

  getMailInfo = () => {
    requestPromise(`${getWebPath('sys/config/saleSetting/getSale')}`, {
      method: 'GET',
    }).then(res => {
      if (res.code === 0) {
        this.setState({
          safe: res.safe,
          loading: false,
        });
      }
    });
  };

  handleOk = e => {
    this.setState({ loading: true });
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        const info = this.props.form.getFieldsValue();
        requestPromise(`${getWebPath('sys/config/saleSetting/save')}`, {
          method: 'POST',
          body: info,
        }).then(res => {
          // if (res.code === 0) {
          //   this.setState({
          //     safe: res.safe,
          //   });
          // }
          message.success('保存成功');
          this.setState({
            loading: false,
          });
        });
      }
    });
  };

  onChange = e => {};

  render() {
    const ItemLayout = {
      labelCol: { span: 3 },
      wrapperCol: { span: 20 }, //5+19=24,少一个栅格
    };
    const formItemLayout = {
      labelCol: { span: 6 },
      wrapperCol: { span: 17 }, //5+19=24,少一个栅格
    };
    const colSpanLayout = { md: 24, sm: 24 }; //每一col跨?列
    const rowGutterPxLayout = { md: 24, sm: 24 }; //col之间的间隔?px
    const {
      form: { getFieldDecorator },
    } = this.props;
    const model = this.state.safe;
    console.log(model);

    return (
      <div layout="vertical" hideRequiredMark>
        <Spin spinning={this.state.loading}>
          <Card title="密码策略">
            <Col {...colSpanLayout}>
              <FormItem label="密码强度:" {...ItemLayout}>
                {getFieldDecorator('pwdStrong', {
                  initialValue: model.pwdStrong,
                  rules: [
                    {
                      required: true,
                      message: '请选择密码强度',
                    },
                  ],
                })(
                  <Select defaultValue="" style={{ width: 350 }}>
                    <Option value="">请选择密码强度</Option>
                    <Option value={0}>只能输入6位数字</Option>
                    <Option value={1}>只能输入6位英文字母</Option>
                    <Option value={2}>6-20位字符，包含大小写、数字、标点符号</Option>
                    <Option value={3}>6-20位字符，只能包含大小写字母、数字、下划线</Option>
                    <Option value={4}>6-20位字符，必须包含大写字母、小写字母、数字</Option>
                  </Select>
                )}
              </FormItem>
            </Col>
            <Col {...colSpanLayout}>
              <FormItem label="密码有效期:" {...ItemLayout}>
                {getFieldDecorator('pwdExpire', {
                  initialValue: model.pwdExpire !== undefined ? model.pwdExpire : '',
                })(<InputNumber min={0} max={180} defaultValue={0} />)}
                <span>&nbsp;天&nbsp;&nbsp;</span>
                <Text type="secondary">
                  0~180。密码过期后用户将无法登录，为0则密码永不过期，默认为0。
                </Text>
              </FormItem>
            </Col>
          </Card>
          <Card title="登录设置">
            <Col {...colSpanLayout}>
              <FormItem label="WEB会话超时:" {...ItemLayout}>
                {getFieldDecorator('sessionTimeout', {
                  initialValue: model.sessionTimeout !== undefined ? model.sessionTimeout : 60,
                })(<InputNumber min={5} max={1440} defaultValue={60} />)}
                <span>&nbsp;分钟 &nbsp;&nbsp;</span>
                <Text type="secondary">
                  5~1440。超过设定时长无操作，用户将被强制登出，默认为60分钟。
                </Text>
              </FormItem>
            </Col>
            <Col {...colSpanLayout}>
              <FormItem label="密码尝试次数:" {...ItemLayout}>
                {getFieldDecorator('retry', {
                  initialValue: model.retry !== undefined ? model.retry : 0,
                })(<InputNumber min={0} max={10} defaultValue={0} />)}
                <span>&nbsp;次 &nbsp;&nbsp;</span>
                <Text type="secondary">
                  0~10。密码连续错误超过设定次数，用户将被临时锁定，为0则不限制，默认为0。
                </Text>
              </FormItem>
            </Col>
            <Col {...colSpanLayout}>
              <FormItem label="临时锁定时长:" {...ItemLayout}>
                {getFieldDecorator('lockTimeout', {
                  initialValue: model.lockTimeout !== undefined ? model.lockTimeout : 30,
                })(<InputNumber min={0} max={9999} defaultValue={30} />)}
                <span>&nbsp;分钟 &nbsp;&nbsp;</span>
                <Text type="secondary">
                  0~9999。用户被临时锁定的持续时间，为0则持续到由管理员解锁，默认为30分钟。（预留）
                </Text>
              </FormItem>
            </Col>
          </Card>
          <Row gutter={{ ...rowGutterPxLayout }}>
            <Col {...colSpanLayout}>
              <Button type="primary" style={{ marginTop: 20 }} onClick={this.handleOk}>
                <FormattedMessage id="safesettings.basic.save" defaultMessage="保存安全设置" />
              </Button>
            </Col>
          </Row>
        </Spin>
      </div>
    );
  }
}

export default Form.create()(SafeSetting);
